package com.up.cloudfeiyibackend.handler;

import com.up.cloudfeiyibackend.exception.BaseException;
import com.up.cloudfeiyibackend.result.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * 全局异常处理器
 */
@Slf4j
@RestControllerAdvice
public class GlobalExceptionHandler {
    /**
     * 运行时异常处理
     *
     * @param e 运行时异常
     * @return
     */
    @ExceptionHandler(RuntimeException.class)
    public Result<String> handleRuntimeException(RuntimeException e) {
        log.error("系统异常: {}", e.getMessage(),e);
        return Result.error(e.getMessage());
    }

    /**
     * 业务异常处理
     *
     * @param e 自定义异常
     * @return
     */
    @ExceptionHandler(BaseException.class)
    public Result<String> handleBaseException(BaseException e) {
        log.error("业务异常: {}", e.getMessage(),e);
        return Result.error(e.getMessage());
    }
}
